package com.huiketong.cofpasgers.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huiketong.cofpasgers.constant.*;
import com.huiketong.cofpasgers.entity.*;
import com.huiketong.cofpasgers.json.response.BaseJsonResponse;
import com.huiketong.cofpasgers.repository.*;
import com.huiketong.cofpasgers.util.DateUtils;
import com.huiketong.cofpasgers.util.MD5Util;
import com.huiketong.cofpasgers.util.ObjectUtils;
import com.huiketong.cofpasgers.util.SerialGeneratorUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
/**
 * 企业信息
 */
public class EnterpriseController {
    @Autowired
    EnterpriseRepository enterpriseRepository;
    @Autowired
    UserRightsRepository userRightsRepository;
    @Autowired
    CompanyUserRepository companyUserRepository;
    @Autowired
    CommissionRepository commissionRepository;
    @Autowired
    BannerRepository bannerRepository;
    @Autowired
    IntegralRuleRepository integralRuleRepository;
    @Autowired
    AgentRepository agentRepository;
    @Autowired
    DefaultEnterRepository defaultEnterRepository;
    @Autowired
    OnlineCollegeRepository onlineCollegeRepository;
    @Autowired
    ShareTypeRepository shareTypeRepository;
    @Autowired
    LinShareRepository linShareRepository;
    @GetMapping(value = "enterprise")
    public ModelAndView Enter(Map<String, Object> map, String user_id) {
        ModelAndView mv = new ModelAndView(Constant.PREFIX + "entermanager");
        return mv;
    }

    @GetMapping(value = "get_eateries")
    @ResponseBody
    public Object GetEnterInfo() {
        List<Enterprise> enterpriseList = enterpriseRepository.findAll();
        Map<String, Object> map = new HashMap<>();
        if (enterpriseList.size() > 0) {
            map.put("data", enterpriseList);
        } else {
            map.put("data", "");
        }
        return map;
    }

    @GetMapping(value = "enterprise_info")
    public Object enterpriseInfo(String user_id, Map<String, Object> map) throws ParseException {
        ModelAndView mv = new ModelAndView(URL.ENTERPRISEINFO);
        Enterprise enterprise = enterpriseRepository.findEnterpriseByEnterLoginName(user_id);
        if (ObjectUtils.isNotNull(enterprise)) {
            map.put("loginname", enterprise.getEnterLoginName());
            map.put("entername", enterprise.getEnterName());
            map.put("enteradress", enterprise.getEnterAddress());
            map.put("enterleader", enterprise.getEnterLegalperson());
            map.put("entertel", enterprise.getEnterTelphone());
            map.put("enterdate", DateUtils.dateFormat(enterprise.getEstablelishDate(), DateUtils.DATE_PATTERN));
            map.put("brokerage", enterprise.getBrokerage());

        }
        return mv;
    }

    @GetMapping(value = "getcommonyinfo")
    @ResponseBody
    @CrossOrigin
    public JSONData GetCommonyinfo(Integer page, Integer limit) {
        JSONData response = new JSONData();
        List<Enterprise> enterpriseList = enterpriseRepository.findPagesEnter((page - 1) * limit, limit);
        if (enterpriseList.size() > 0) {
            response.setCode(0);
            response.setData(enterpriseList);
            response.setCount((int) enterpriseRepository.count());
            response.setMsg("");
        } else {
            response.setCode(0);
            response.setData(new ArrayList<>());
            response.setCount(0);
            response.setMsg("");
        }
        return response;
    }

    @PostMapping(value = "companyinfo")
    @ResponseBody
    public Enterprise ShowCompany(HttpServletRequest request) {
        String login_id = request.getParameter("login_id");
        Enterprise enterprise = enterpriseRepository.findEnterpriseByEnterLoginName(login_id);
        if (enterprise != null) {
            return enterprise;
        }
        return null;
    }

    /**
     * 添加公司
     *
     * @return
     */
    @GetMapping(value = "/addenter")
    public String AddEnter() {
        return URL.CREATEENTERPRISE;
    }

    /**
     * 修改公司
     *
     * @param id
     * @param map
     * @return
     */
    @GetMapping(value = "modifyenter")
    public ModelAndView ModifyEnter(Integer id, Map<String, Object> map) throws ParseException {
        ModelAndView mv = new ModelAndView(Constant.PREFIX + "modifyEnter");
        Enterprise enterprise = enterpriseRepository.findEnterpriseById(id);
        map.put("enterId", enterprise.getId());
        map.put("enterName", enterprise.getEnterName());
        map.put("enterAddress", enterprise.getEnterAddress());
        map.put("enterLegal", enterprise.getEnterLegalperson());
        map.put("enterTelphone", enterprise.getEnterTelphone());
        String enterEstableish = DateUtils.dateFormat(enterprise.getEstablelishDate(), DateUtils.DATE_PATTERN);
        map.put("enterEstablelish", enterEstableish);
        if (!ObjectUtils.isEmpty(enterprise.getEnterLogo())) {
            map.put("enterLogo", Constant.IMAGE_URL + enterprise.getEnterLogo());
        } else {
            map.put("enterLogo", "/images/add.png");
        }

        map.put("enterSort", enterprise.getEnterOrder());
        map.put("enterStatus", enterprise.getEnterStatus());
        map.put("enterPerson", enterprise.getPersonOnline());
        map.put("enterComment", enterprise.getEntercomment());
        return mv;
    }

//    @PostMapping(value = "agentcompanyinfo")
//    @ResponseBody
//    public Enterprise ShowAgentCompanyinfo(HttpServletRequest request) {
//        String login_id = request.getParameter("user_id");
//        CompanyBindUser companyBindUser = companyUserRepository.findCompanyBindUserByUserTel(login_id);
//        Enterprise enterprise = enterpriseRepository.findEnterpriseById(companyBindUser.getCompanyId());
//        if (enterprise != null) {
//            return enterprise;
//        }
//        return null;
//    }

    @GetMapping(value = "company")
    public ModelAndView showCompany() {
        ModelAndView mv = new ModelAndView(Constant.PREFIX + "company");
        return mv;
    }

    @PostMapping(value = "get_current_com_info")
    @ResponseBody
    public Enterprise GetCurentComInfo(HttpServletRequest request) {
        String login_id = request.getParameter("user_id");
        Enterprise enterprise = enterpriseRepository.findEnterpriseByEnterLoginName(login_id);
        return enterprise;
    }

    @PostMapping(value = "addeter")
    @ResponseBody
    public BaseJsonResponse AddAddeter(HttpServletRequest request) throws ParseException, UnsupportedEncodingException, NoSuchAlgorithmException {
        BaseJsonResponse response = new BaseJsonResponse();
        String enterName = request.getParameter("enter_name");
        String enterAddress = request.getParameter("enter_address");
        String enterLegal = request.getParameter("enter_legal");
        String enterLogo = request.getParameter("logo");
        String enterOrder = request.getParameter("enter_order");
        String enterStatus = request.getParameter("enter_status");
        String enterTelphone = request.getParameter("enter_telphone");
        String establelishDate = request.getParameter("establish_date");
        String personOnline = request.getParameter("person_num");
        String comment = request.getParameter("comment_id");
        String loginname = request.getParameter("enter_loginame");
        if (ObjectUtils.isEmpty(enterName)) {
            response.setCode("3").setMsg("企业名不能为空");
        } else if (ObjectUtils.isEmpty(enterAddress)) {
            response.setCode("3").setMsg("企业地址不能为空");
        } else if (ObjectUtils.isEmpty(enterLegal)) {
            response.setCode("3").setMsg("企业法人不能为空");
        } else if (ObjectUtils.isEmpty(enterLogo)) {
            response.setCode("3").setMsg("企业Logo不能为空");
        } else if (ObjectUtils.isEmpty(enterOrder)) {
            response.setCode("3").setMsg("企业排序不能为空");
        } else if (ObjectUtils.isEmpty(enterStatus)) {
            response.setCode("3").setMsg("企业状态不能为空");
        } else if (ObjectUtils.isEmpty(enterTelphone)) {
            response.setCode("3").setMsg("企业电话不能为空");
        } else if (ObjectUtils.isEmpty(establelishDate)) {
            response.setCode("3").setMsg("企业成立日期不能为空");
        } else if (ObjectUtils.isEmpty(personOnline)) {
            response.setCode("3").setMsg("企业人数上线不能为空");
        } else if (ObjectUtils.isEmpty(comment)) {
            response.setCode("3").setMsg("企业id不能为空");
        } else if (ObjectUtils.isEmpty(loginname)) {
            response.setCode("0").setMsg("登录名不能为空");
        } else {
            Enterprise enterprise2 = enterpriseRepository.findEnterpriseByEnterLoginName(loginname);
            if (!ObjectUtils.isEmpty(enterprise2)) {
                response.setCode("3").setMsg("该账号已经注册过企业");
            } else {
                Enterprise enterprise = new Enterprise();
                enterprise.setEnterName(enterName);
                enterprise.setEnterAddress(enterAddress);
                enterprise.setEnterLegalperson(enterLegal);
                enterprise.setEnterLogo(enterLogo);
                enterprise.setEnterOrder(Integer.parseInt(enterOrder));
                enterprise.setEnterStatus(Integer.parseInt(enterStatus));
                enterprise.setEnterTelphone(enterTelphone);
                enterprise.setEntercomment(comment);
                //随机字符串
//              String loginname = RandomStringUtils.random(8, "1234567890");
                //排重
//              loginname = Distinct(loginname);
                UserRights existRight = userRightsRepository.findUserRightsByUserTelAndUserRightOrLoginNameAndUserRight(loginname, UserType.COMPANY.ordinal(), loginname, UserType.COMPANY.ordinal());
                if (ObjectUtils.isEmpty(existRight)) {
                    UserRights rights = new UserRights();
                    rights.setLoginName(loginname);
                    rights.setUserRight(UserType.COMPANY.ordinal());
                    rights.setRightName("装饰公司");
                    userRightsRepository.save(rights);
                }
                enterprise.setEnterLoginName(loginname);
                enterprise.setBrokerage(0);
                enterprise.setEnterLoginPwd(MD5Util.getEncryptedPwd("123456"));
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                enterprise.setEstablelishDate(format.parse(establelishDate));
                enterprise.setPersonOnline(Integer.parseInt(personOnline));
                enterpriseRepository.save(enterprise);
                Enterprise enterprise1 = enterpriseRepository.findEnterpriseByEnterLoginName(loginname);
                if (ObjectUtils.isNotNull(enterprise1)) {
                    Commission commission = new Commission();
                    commission.setUpdateTime(new Date());
                    commission.setScore(new BigDecimal(0));
                    commission.setCompanyId(enterprise1.getId());
                    commission.setInvitScore(new BigDecimal(0));
                    commission.setFirstPercentage(0.0f);
                    commission.setSecondPercentage(0.0f);
                    commission.setMinWithdraw(new BigDecimal(100));
                    commission.setBWithdrawOpen(1);
                    commission.setShareCount(5);
                    commission.setOfirstPercentage(0.0f);
                    commission.setOsecondPercentage(0.0f);
                    commissionRepository.save(commission);
                    for (int i = 0; i < 3; i++) {
                        Banner banner = new Banner();
                        if (i == 0) {
                            banner.setImgurl("http://image.yzcang.com/hd/banner.png");

                        } else {
                            banner.setImgurl("http://image.yzcang.com/hd/banner" + (i + 1) + ".png");
                        }
                        banner.setTrankurl("");
                        banner.setDescript("");
                        banner.setCreateDate(new Date());
                        banner.setStatus(1);
                        banner.setName("默认轮播图");
                        banner.setSort(i + 1);
                        banner.setCompanyId(enterprise1.getId());
                        bannerRepository.save(banner);
                    }

                    JSONArray jsonArray = new JSONArray();
                    Integer[] dials = {10, 10, 10, 10, 10, 10, 10, 10};
                    Integer[] probability = {10, 20, 30, 5, 5, 10, 10, 10};
                    for (int i = 0; i < dials.length; i++) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("id", i);
                        jsonObject.put("integral", dials[i]);
                        jsonObject.put("probability", probability[i]);
                        jsonArray.add(jsonObject);
                    }
                    Agent agent = new Agent();
                    agent.setAgentName(enterName);
                    String inviteCode = SerialGeneratorUtil.GetRandomString(8);
                    agent.setInitCode(inviteCode);
                    agent.setType(AgentType.CUSERVICE.ordinal());
                    agent.setTelphone(enterTelphone);
                    agent.setLoginUsername(loginname);
                    agent.setPassword(MD5Util.getEncryptedPwd("123456"));
                    agent.setBCertification(0);
                    agent.setSuperiorName("");
                    agent.setSuperId(0);
                    agent.setTopName("");
                    agent.setTopId(0);
                    agent.setInitAgentNam(0);
                    agent.setReconCustomNam(0);
                    agent.setPoints(0);
                    agent.setCardPhoto("");
                    agent.setDealCustomNum(0);
                    agent.setForbid(0);
                    agent.setDrawlPad(MD5Util.getEncryptedPwd("123456"));
                    agent.setIdCard("");
                    agent.setImposer(false);
                    agent.setInitAgentNam(0);
                    agent.setRealName("");
                    agent.setDeviceId("");
                    agent.setAccountBalance(new BigDecimal(0));
                    agent.setCompanyId(enterprise1.getId());
                    agent.setAvatar(enterLogo);
                    agentRepository.save(agent);

                    CompanyBindUser companyBindUser = new CompanyBindUser();
                    companyBindUser.setCompanyId(enterprise.getId());
                    companyBindUser.setInviteCode(inviteCode);
                    companyBindUser.setUserId(null);
                    companyBindUser.setUserTel(enterTelphone);
                    companyUserRepository.save(companyBindUser);

                    UserRights existagentRight = userRightsRepository.findUserRightsByUserTelAndUserRightOrLoginNameAndUserRight(loginname, UserType.AGENT.ordinal(), loginname, UserType.AGENT.ordinal());
                    if(ObjectUtils.isEmpty(existagentRight)) {
                        UserRights agentrights = new UserRights();
                        agentrights.setLoginName(loginname);
                        agentrights.setUserRight(UserType.AGENT.ordinal());
                        agentrights.setRightName("经纪人");
                        userRightsRepository.save(agentrights);
                    }


                    for(int i = 1;i<=4;i++) {
                        ShareType shareType = new ShareType();
                        LinShare linShare = new LinShare();
                        switch (i){
                            case 1:
                                shareType.setCompanyId(enterprise.getId());
                                shareType.setImgUrl("http://image.jkt365.com/share_imag2.jpg");
                                shareType.setUrl(null);
                                shareType.setTitle(null);
                                shareType.setType(1);

                                linShare.setCompanyId(enterprise.getId());
                                linShare.setContent("元搞定全屋设计");
                                linShare.setUrl("westinghouse#/pages/index/ad_01");
                                linShare.setImgUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573816287251&di=f32c62fe0965bc3b681eb7f85e46a6fe&imgtype=0&src=http%3A%2F%2Fimg1n.soufunimg.com%2Fviewimage%2Fjiancai%2Fbusiness%2Fto8to%2F201508%2F21%2F651%2F14535dbe140884477635ec77594");
                                linShare.setType(1);
                                linShare.setTitle("0元搞定全屋设计");
                                linShare.setTopimg("http://image.yzcang.com/jiketong/images/ad_01_1.jpg");
                                linShare.setBottomimg("http://image.yzcang.com/jiketong/images/ad_01_2.jpg");
                                linShare.setBackgroud("faecdf");
                                linShare.setTime(new Date());

                                break;
                            case 2:
                                shareType.setCompanyId(enterprise.getId());
                                shareType.setImgUrl("http://image.jkt365.com/share_imag3.jpg");
                                shareType.setUrl(null);
                                shareType.setTitle(null);
                                shareType.setType(2);

                                linShare.setCompanyId(enterprise.getId());
                                linShare.setContent("装修预算");
                                linShare.setUrl("warehouses#/pages/index/ad_02");
                                linShare.setImgUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573816287251&di=f32c62fe0965bc3b681eb7f85e46a6fe&imgtype=0&src=http%3A%2F%2Fimg1n.soufunimg.com%2Fviewimage%2Fjiancai%2Fbusiness%2Fto8to%2F201508%2F21%2F651%2F14535dbe140884477635ec77594");
                                linShare.setType(2);
                                linShare.setTitle("装修预算");
                                linShare.setTopimg(null);
                                linShare.setBottomimg(null);
                                linShare.setBackgroud(null);
                                linShare.setNum(321312);
                                linShare.setTime(new Date());
                                break;
                            case 3:
                                shareType.setCompanyId(enterprise.getId());
                                shareType.setImgUrl("http://image.jkt365.com/share_imag4.jpg");
                                shareType.setUrl(null);
                                shareType.setTitle(null);
                                shareType.setType(3);

                                linShare.setCompanyId(enterprise.getId());
                                linShare.setContent("样板房免费接驾到店");
                                linShare.setUrl("roompolys#/pages/index/ad_03");
                                linShare.setImgUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573816287251&di=f32c62fe0965bc3b681eb7f85e46a6fe&imgtype=0&src=http%3A%2F%2Fimg1n.soufunimg.com%2Fviewimage%2Fjiancai%2Fbusiness%2Fto8to%2F201508%2F21%2F651%2F14535dbe140884477635ec77594");
                                linShare.setType(3);
                                linShare.setTitle("样板房免费接驾到店");
                                linShare.setTopimg("http://image.yzcang.com/jiketong/images/ad_03_1.jpg");
                                linShare.setBottomimg("http://image.yzcang.com/jiketong/images/ad_03_2.jpg");
                                linShare.setBackgroud("b4daed");
                                linShare.setTime(new Date());
                                break;
                            case 4:
                                shareType.setCompanyId(enterprise.getId());
                                shareType.setImgUrl("http://image.jkt365.com/share_imag1.jpg");
                                shareType.setUrl(null);
                                shareType.setTitle(null);
                                shareType.setType(4);
                                break;
                        }
                        linShareRepository.save(linShare);
                        shareTypeRepository.save(shareType);
                    }


                    DefaultEnter existDefaultEnter = defaultEnterRepository.findDefaultEnterByUserId(loginname);
                    if(ObjectUtils.isEmpty(existDefaultEnter)){
                        DefaultEnter defaultEnter = new DefaultEnter();
                        defaultEnter.setCompayId(enterprise1.getId());
                        defaultEnter.setCompayName(enterprise1.getEnterName());
                        defaultEnter.setUserId(loginname);
                        defaultEnter.setUserTelphone(loginname);
                        defaultEnterRepository.save(defaultEnter);
                    }
                    // System.out.println(jsonArray);
                    IntegralRule integralRule = new IntegralRule();
                    integralRule.setCompanyId(enterprise1.getId());
                    integralRule.setMinPrice(10);
                    integralRule.setRmbForPoint(10);
                    integralRule.setIsPoint(2);
                    integralRule.setIsOpenLotto(2);
                    integralRule.setConsume(20);
                    integralRule.setLoginIntegral(1);
                    integralRule.setSignIntegral(1);
                    integralRule.setRecomIntegral(1);
                    integralRule.setAuthIntegral(1);
                    integralRule.setInviteIntegral(1);
                    integralRule.setDials(jsonArray.toString());
                    integralRuleRepository.save(integralRule);


                    OnlineCollege onlineCollege = new OnlineCollege();
                    onlineCollege.setCompanyId(enterprise.getId());
                    onlineCollege.setArticleType(1);
                    onlineCollege.setCreateTime(new Date());
                    onlineCollege.setParticulars("<h1>\n" +
                            "\t<strong>集客通APP使用手册</strong> \n" +
                            "</h1>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a1\">1 系统简介</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a1_1\">1.1 APP客户端简介</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a1_2\">1.2 系统使用对象</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a2\">2 首页</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a2_1\">2.1 首页轮播</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a2_2\">2.2 切换账号</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a2_3\">2.3 报备客户</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a2_4\">2.4 我的佣金</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a2_5\">2.5 客户管理</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a2_6\">2.6 邀请好友</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a2_7\">2.7 AI客服</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a2_8\">2.8 整装案例</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a2_9\">2.9 扩客工具</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<a href=\"#a2_10\">2.10 用户设置</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<br />\n" +
                            "</p>\n" +
                            "<h3>\n" +
                            "\t<span style=\"font-size:16px;\"><strong><a name=\"a1\"></a>1 系统简介</strong></span> \n" +
                            "</h3>\n" +
                            "<p>\n" +
                            "\t<span style=\"font-size:14px;\"><strong><a name=\"a1_1\"></a>1.1 APP客户端简介</strong></span> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\tios版本可以通过直接在苹果app store应用商店里输入【集客通】进行搜索下载安装。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\tandroid版本可以通过华为应用市场，应用宝里输入【集客通】进行搜索下载安装。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t通用下载地址：<a href=\"https://sj.qq.com/myapp/detail.htm?apkName=com.huiketong.jiketong\" target=\"_blank\">https://sj.qq.com/myapp/detail.htm?apkName=com.huiketong.jiketong</a> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t安装完成后，点击运行客户端，在登录界面输入用户名、密码，进入手机客户端主界面。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.yzcang.com/static/install/jiketong/image/explain/1.1.1.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126092852_432.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126092907_526.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<strong><span style=\"font-size:14px;\"><a name=\"a1_2\"></a>1.2 系统使用对象</span></strong> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\tapp使用的对象为企业经纪人\n" +
                            "</p>\n" +
                            "<h3>\n" +
                            "\t<span style=\"font-size:16px;\"><a name=\"a2\"></a>2 首页</span> \n" +
                            "</h3>\n" +
                            "<p>\n" +
                            "\t<strong><span style=\"font-size:14px;\"><a name=\"a2_1\"></a>2.1 首页轮播</span></strong> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t首页最上方区域为当前登录经纪人对应的企业罪行的轮播广告\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126093120_143.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t点击app首页轮播图片，如果改链接在后台设置的是活动推广页面链接，则跳转到活动详情界面，点击右上角的【分享图标按钮】，分享到微信社交平台。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t注意：系统会根据app登录的经纪人账号识别，是哪个经纪人分享得链接，后续推广的下线经纪人或者推荐的客户都归属于该经纪人下面；\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t否则非活动推广页面链接，为外部url链接的情况下，就打开展示页面作为宣传，不存在邀请经纪人在线注册或者客户在线报备。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<br />\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t活动推广页面\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.yzcang.com/static/install/jiketong/image/explain/2.1.2.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t（app展示页）\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<br />\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t在微信中点击，如果是【邀请经纪人活动】则会在下方显示【注册成为经纪人】按钮，点击按钮打开经纪人注册界面，会显示邀请经纪人的邀请码，以及对应的经纪人公司。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t如果是【推荐客户活动】则会在下方显示【<span>我要预约装修</span>】按钮，点击按钮就会跳转装修预约页面，同时会自动显示推荐人（经纪人姓名）；\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<br />\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<strong><span style=\"font-size:14px;\"><a name=\"a2_2\"></a>2.2 切换账号</span></strong> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t一个经纪人同一个手机号码可以同时注册到不同企业下，可以通过【切换账号】来切换不同企业下的账号；\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.yzcang.com/static/install/jiketong/image/explain/2.2.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<strong><span style=\"font-size:14px;\"><a name=\"a2_3\"></a>2.3 报备客户</span></strong> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t1、经纪人主动报备客户，在首页点击【推荐客户】进行填写客户资料。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126093328_468.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t2、经纪人通过活动推广页面分享到微信，让客户填写资料进行报备。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t注意：系统会根据分享活动链接的经纪人，在客户预约的页面展示经纪人姓名。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<br />\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<strong><span style=\"font-size:14px;\"><a name=\"a2_4\"></a>2.4 我的佣金</span></strong> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t点击底部导航【我的】，然后点击【账户】，进入我的佣金界面。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.yzcang.com/static/install/jiketong/image/explain/2.4.1.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t展示经纪人对应账户的余额，该账户余额 = 佣金奖励总和 - 提现总额；\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【收益明细】可以查看到所有佣金的入账明细；\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【提现明细】佣金的提现记录；\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【银行卡】佣金到帐银行卡的设置；\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【提现】进入提现界面进行佣金提现；\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<strong><span style=\"font-size:14px;\"><a name=\"a2_5\"></a>2.5 客户管理</span></strong> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t点击底部导航【客户】进入客户管理\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126093635_680.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【客户意向】查看经纪人自己和下线经纪人报备的客户信息，客户状态分为待审核、期房、暂时不装、已量房、已进店、跟踪中、已交定金、已签合同；\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t经纪人可以通过该页面实时了解客户的当前进度，如果客户状态为已签合同，则会产生对应的佣金奖励，在【收益明细】里会提现出来。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【在线学院】可以查看公司独有的扩客技巧。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<strong><span style=\"font-size:14px;\"><a name=\"a2_6\"></a>2.6 邀请好友</span></strong> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t点击首页的【邀请好友】，进入邀请好友界面，可以进行面对面扫码注册。也可以点击【立即邀请】按钮通过微信分享给好友。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126093648_754.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<strong><span style=\"font-size:14px;\"><a name=\"a2_7\"></a>2.7 AI客服</span></strong> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t点击底部导航的【AI客服】可以进入客服聊天界面。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t如果是普通经纪人，则可以和公司客服进行一对一聊天。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t如果是公司制定的客服经纪人，则可以显示公司下的所有经纪人，并能和他们一对一进行聊天\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126093657_475.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t而且当经纪人进行报备客户的时候，还能实时收到系统消息，查看报备客户的信息\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.yzcang.com/static/install/jiketong/image/explain/2.7.2.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<br />\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<strong><span style=\"font-size:14px;\"><a name=\"a2_8\"></a>2.8 整装案例</span></strong> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t首页可以展示公司的案例信息，点击案例进入案例详情页面\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126093735_329.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126093745_197.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t经纪人可以通过右上角的分享按钮微信分享给好友；\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<br />\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<strong><span style=\"font-size:14px;\"><a name=\"a2_9\"></a>2.9 扩客工具</span></strong> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t首页可以展示后台设置的扩客工具，方便企业进行扩客。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126094202_644.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126094215_120.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126094230_888.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<h4>\n" +
                            "\t<strong>2.9.1 优惠券</strong> \n" +
                            "</h4>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.jkt365.com/image/20191126/20191126094519_477.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t经纪人可以点击【立即分享】通过微信分享给好友，好友填写资料领取优惠券，填写资料后会收到一条短信，凭此短信到点享受优惠；\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t也可以通过点击【立即推荐】主动填写有意向客户信息。\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<br />\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<strong><span style=\"font-size:14px;\"><a name=\"a2_10\"></a>2.10 用户设置</span></strong> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t点击底部导航【我的】 -&gt; 【设置】\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<br />\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<img src=\"http://image.yzcang.com/static/install/jiketong/image/explain/2.10.1.jpg\" alt=\"\" /> \n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【头像】设置经纪人自己喜欢的头像\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【昵称】经纪人可以设置自己个性的称谓\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【提现密码】修改提现时候的密码\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【登录密码】修改登录时候的密码\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【更换手机号】可以更改当前账户绑定的手机号\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t【联系客服】点击可以直接给公司客服拨打电话\n" +
                            "</p>\n" +
                            "<p>\n" +
                            "\t<br />\n" +
                            "</p>");
                    onlineCollege.setTitle("APP使用手册");
                    onlineCollege.setVideoIntro("");
                    onlineCollege.setVideoUrl("");
                    onlineCollegeRepository.save(onlineCollege);
                    response.setCode("1").setMsg("添加企业成功");
                }
            }
        }
        return response;
    }

    /**
     * 排重
     *
     * @param loginname
     * @return
     */
    String Distinct(String loginname) {
        String tempname = loginname;
        Enterprise enterprise = enterpriseRepository.findEnterpriseByEnterLoginName(tempname);
        if (enterprise != null) {
            tempname = RandomStringUtils.random(8, "1234567890");
            Distinct(tempname);
        }
        return tempname;
    }

    @PostMapping(value = "modifyenterinfo")
    @ResponseBody
    public Object modifyEnterInfo(Integer brokerage, String enteradress, String enterleader, String enterdate,
                                  String entername, String entertel, String loginname, String loginpwd, String loginrpwd) {
        BaseJsonResponse response = new BaseJsonResponse();
        if (ObjectUtils.isNotEmpty(brokerage) && ObjectUtils.isNotEmpty(enteradress) && ObjectUtils.isNotEmpty(enterleader) &&
                ObjectUtils.isNotEmpty(enterdate) && ObjectUtils.isNotEmpty(entername) && ObjectUtils.isNotEmpty(entertel) && ObjectUtils.isNotEmpty(loginname)) {
            try {
                if (ObjectUtils.isNotEmpty(loginpwd)) {
                    if (ObjectUtils.isNotEmpty(loginrpwd) && !loginpwd.equals(loginrpwd)) {
                        response.setMsg("输入密码不一致").setCode("3");
                    } else {
                        String md5pwd = MD5Util.getEncryptedPwd(loginpwd);
                        enterpriseRepository.updateEnterPwd(md5pwd, loginname);
                    }
                }
                enterpriseRepository.updateEnterInfo(brokerage, enteradress, enterleader, entername, entertel, DateUtils.dateParse(enterdate, DateUtils.DATE_PATTERN), loginname);
                response.setMsg("修改企业信息成功").setCode("1");

            } catch (Exception e) {
                response.setMsg("修改企业信息异常").setCode("400");
            }

        } else {
            response.setCode("2").setMsg("修改的公司信息不完善");
        }
        return response;
    }

    @PostMapping(value = "update_enter_status")
    @ResponseBody
    public BaseJsonResponse updateEnterStatus(String enter_login_name, Integer status) {
        BaseJsonResponse response = new BaseJsonResponse();
        try {
            enterpriseRepository.updateEnterStatus(enter_login_name, status);
            response.setCode("0").setMsg("修改状态成功");
        } catch (Exception e) {
            response.setCode("1").setMsg("修改状态失败");
        }

        return response;
    }
}
